package ca.jerg.config;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;

public class ErgConfigWorkTime implements IErgConfig
{
	/*
	 * Default work intensity description.
	 */
	public static final double DEF_ANAEROBIC_TIME = 1.5;
	public static final double DEF_VO2_TIME = 5.0;
	public static final double DEF_FT_TIME = 12.5;
	public static final double DEF_TEMPO_TIME = 25;
	public static final double DEF_DISTANCE_TIME = 40;
	public static final double DEF_RAMP_TIME = 0.3;		// set to 20 seconds
	
	private Preferences config;
	private final String ERG_CFG_WORK_TIME_STR = "ErgCfgWorkTime: ";	

	public ErgConfigWorkTime()
	{
		double d;
		
		Preferences p = Preferences.userNodeForPackage(ErgConfigWork.class);
		config = p.node("WorkTimes");
		
		/*
		 * The first time this class is loaded there is no
		 * stored Work Load configuration.  If, that is
		 * the case then create a setup using the default
		 * values.
		 */
		d = config.getDouble(KEY_ANAEROBIC,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_ANAEROBIC,DEF_ANAEROBIC_TIME);

		d = config.getDouble(KEY_VO2,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_VO2,DEF_VO2_TIME);

		d = config.getDouble(KEY_FT,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_FT,DEF_FT_TIME);

		d = config.getDouble(KEY_TEMPO,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_TEMPO,DEF_TEMPO_TIME);
		
		d = config.getDouble(KEY_DISTANCE,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_DISTANCE,DEF_DISTANCE_TIME);
		
		d = config.getDouble(KEY_RAMP,-1.0);
		if ( d == -1.0 )
			config.putDouble(KEY_RAMP,DEF_RAMP_TIME);
	}
	
	//@Override
	public void defaults()
	{
		config.putDouble(KEY_ANAEROBIC,DEF_ANAEROBIC_TIME);
		config.putDouble(KEY_VO2,DEF_VO2_TIME);
		config.putDouble(KEY_FT,DEF_FT_TIME);
		config.putDouble(KEY_TEMPO,DEF_TEMPO_TIME);
		config.putDouble(KEY_DISTANCE,DEF_DISTANCE_TIME);
		config.putDouble(KEY_RAMP,DEF_RAMP_TIME);
	}

	//@Override
	public boolean exportConfig( String path )
	{
		try
		{
			OutputStream os = new BufferedOutputStream(new FileOutputStream(path));
			config.exportSubtree(os);
			os.close();
		}
		catch ( FileNotFoundException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "Unable to create file");
			return false;
		}
		catch ( IOException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "IO error");
			return false;
		}
		catch ( BackingStoreException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "Platform error");
			return false;
		}
		
		return true;
	}

	//@Override
	public boolean getBoolean( String key, boolean def )
	{
		// no boolean values
		return false;
	}

	//@Override
	public double getDouble( String key, double def )
	{		
		return config.getDouble(key,def);
	}

	//@Override
	public int getInt( String key, int def )
	{
		// no int values
		return 0;
	}

	//@Override
	public String getString( String key, String def )
	{
		// no string values
		return null;
	}

	//@Override
	public boolean importConfig( String path )
	{
		try
		{
			InputStream is = new BufferedInputStream(new FileInputStream(path));
			Preferences.importPreferences(is);
			is.close();
		}
		catch ( FileNotFoundException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "Unable to locate file");
			return false;
		}
		catch ( IOException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "IO error");
			return false;
		}
		catch ( InvalidPreferencesFormatException e )
		{
			System.err.println(ERG_CFG_WORK_TIME_STR + "Bad data file format");
			return false;
		}
		
		return true;
	}

	//@Override
	public void put( String key, String value )
	{
		// no string values
	}

	//@Override
	public void putBoolean( String key, boolean value )
	{
		// no boolean values
	}

	//@Override
	public void putDouble( String key, double value )
	{
		config.putDouble(key,value);
	}

	//@Override
	public void putInt( String key, int value )
	{
		// no int values
	}

}
